package edu.hsbremen.teamscore.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import edu.hsbremen.teamscore.R;
import edu.hsbremen.teamscore.controller.ChangeIntOnObject;
import edu.hsbremen.teamscore.events.TimerObserver;
import edu.hsbremen.teamscore.ui.UiCountdownTimer;


public class ScreenFloorball extends Activity {
	
	boolean counterIsRunning = false;
	boolean counterStopped = false;
	
	int penaltiesTeam1 = 0;
	int penaltiesTeam2 = 0;
	
	String currentTime = "";
	
	Button goalsTeam1;
	Button goalsTeam2;
	Button startstop;
	
	CountdownObserver countdownObserver;
	UiCountdownTimer mainTimer;
	TextView thirds;
	int third = 1;
	
	TextView team1;
	TextView team2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.floorball);
		
		startstop = (Button) findViewById(R.id.floorball_start_stop_btn);
		Button penaltyTeam1 = (Button) findViewById(R.id.floorball_team1_penaltybutton);
		Button penaltyTeam2 = (Button) findViewById(R.id.floorball_team2_penaltybutton);
		goalsTeam1 = (Button) findViewById(R.id.floorball_team1_goals_btn);
		goalsTeam2 = (Button) findViewById(R.id.floorball_team2_goals_btn);
		
		goalsTeam1.setText("0");
		goalsTeam2.setText("0");
		
		
		team1 = (TextView) findViewById(R.id.floorball_team1_name);
		team2 = (TextView) findViewById(R.id.floorball_team2_name);
		thirds = (TextView) findViewById(R.id.floorball_third_counter);
		thirds.setText("1 / 3");
		
		mainTimer = (UiCountdownTimer) findViewById(R.id.floorball_uiCountdownTimer);
		final UiCountdownTimer penalty1Team1 = (UiCountdownTimer) findViewById(R.id.floorball_team1_penalty1_uiCountdownTimer);
		final UiCountdownTimer penalty2Team1 = (UiCountdownTimer) findViewById(R.id.floorball_team1_penalty2_uiCountdownTimer);
		final UiCountdownTimer penalty1Team2 = (UiCountdownTimer) findViewById(R.id.floorball_team2_penalty1_uiCountdownTimer);
		final UiCountdownTimer penalty2Team2 = (UiCountdownTimer) findViewById(R.id.floorball_team2_penalty2_uiCountdownTimer);
		
		Bundle namesBundle = getIntent().getExtras();
		team1.setText(namesBundle.getString("team1"));
		team2.setText(namesBundle.getString("team2"));
		
		countdownObserver = new CountdownObserver(10, this);
		mainTimer.addTimerEvent(countdownObserver);
		
		startstop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(counterIsRunning == true){
					Log.d("CURRENTIME", currentTime);
					startstop.setBackgroundResource(R.drawable.start1);
					penalty1Team1.stopCountdown();
					penalty2Team1.stopCountdown();
					penalty1Team2.stopCountdown();
					penalty2Team2.stopCountdown();
					mainTimer.stopCountdown();
					counterIsRunning = false;
				}else {
					mainTimer.startCountdown();
					startstop.setBackgroundResource(R.drawable.stop1);
					if(penaltiesTeam1 == 1){
						penalty1Team1.startCountdown();
					} else if(penaltiesTeam1 == 2){
						penalty1Team1.startCountdown();
						penalty2Team1.startCountdown();
					}
					
					if(penaltiesTeam2 == 1){
						penalty1Team2.startCountdown();
					} else if(penaltiesTeam2 == 2){
						penalty1Team2.startCountdown();
						penalty2Team2.startCountdown();
					}
					counterIsRunning = true;
				}
			}
		});
		
		goalsTeam1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				goalsTeam1.setText(new ChangeIntOnObject().countUp(goalsTeam1));
			}
		});
		
		goalsTeam1.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				goalsTeam1.setText(new ChangeIntOnObject().countDown(goalsTeam1));
				return true;
			}
		});
		
		goalsTeam2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				goalsTeam2.setText(new ChangeIntOnObject().countUp(goalsTeam2));				
			}
		});
		
		goalsTeam2.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				goalsTeam2.setText(new ChangeIntOnObject().countDown(goalsTeam2));
				return true;
			}
		});

		penaltyTeam1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch(penaltiesTeam1){
					case 0:
						penalty1Team1.setVisibility(1);
						if(counterIsRunning == true){
							penalty1Team1.startCountdown();
						}
						penaltiesTeam1 = 1;
						break;
					case 1:
						penalty2Team1.setVisibility(1);
						if(counterIsRunning == true){
							penalty2Team1.startCountdown();
						}
						penaltiesTeam1 = 2;
						break;
					case 2:
						Toast toast = Toast.makeText(ScreenFloorball.this, "This Application only allows up to two penalties at a time for each team.", Toast.LENGTH_LONG);
						toast.show();
						break;	
				}
			}
		});
		
		penaltyTeam2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				switch(penaltiesTeam2){
				case 0:
					penalty1Team2.setVisibility(1);
					if(counterIsRunning == true){
						penalty1Team2.startCountdown();
					}
					penaltiesTeam2 = 1;
					break;
				case 1:
					penalty2Team2.setVisibility(1);
					if(counterIsRunning == true){
						penalty2Team2.startCountdown();
					}
					penaltiesTeam2 = 2;
					break;
				case 2:
					Toast toast = Toast.makeText(ScreenFloorball.this, "This Application only allows up to two penalties at a time for each team.", Toast.LENGTH_LONG);
					toast.show();
					break;
				}
			}
		});
	}
	
	public void incrementThird(){
		Log.d("THIRDS", thirds.getText().toString());
		if(third == 1){
			thirds.setText("2 / 3");
			third = 2;
		} else if(third == 2){
			thirds.setText("3 / 3");
			third = 3;
		} else {
			checkWinner();
		}
	}
	
	public void checkWinner(){
		String goalsToWin1 = goalsTeam1.getText().toString();
		int goalsToWin11 = Integer.parseInt(goalsToWin1);
		String goalsToWin2 = goalsTeam1.getText().toString();
		int goalsToWin122 = Integer.parseInt(goalsToWin2);
		
		Log.d("TEAM1", "" + goalsToWin11);
		Log.d("TEAM1", "" + goalsToWin122);


		if (goalsToWin11 > goalsToWin122){
			Toast toast = Toast.makeText(ScreenFloorball.this, "The einner of this game is: " + team1.getText(), Toast.LENGTH_LONG);
			toast.show();
		} else if (goalsToWin122 > goalsToWin11){
			Toast toast = Toast.makeText(ScreenFloorball.this, "The einner of this game is: " + team2.getText(), Toast.LENGTH_LONG);
			toast.show();
		} else {
			Toast toast = Toast.makeText(ScreenFloorball.this, "TIE. The next point wins", Toast.LENGTH_LONG);
			toast.show();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_floorball, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.floorball_rules:
	    	Intent Rules = new Intent(ScreenFloorball.this, Descriptions.class);
	    	Rules.putExtra("game", "floorball");
	    	startActivity(Rules);
	    	return true;
	    case R.id.floorball_backtomain:
	    	this.finish();
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	private class CountdownObserver implements TimerObserver{
		Activity activity;
		long time = 0;
		
		public CountdownObserver(long t, Activity act){
			this.activity = act;
			this.time = t;
		}

		@Override
		public long getMillisWhenShouldHappen() {
			return this.time;
		}

		@Override
		public void update() {
			
			Toast toast = Toast.makeText(ScreenFloorball.this, "Third is over!", Toast.LENGTH_LONG);
			toast.show();
			
			incrementThird();
			mainTimer.stopCountdown();
			startstop.setBackgroundResource(R.drawable.start1);
			mainTimer.resetCountdown();
			mainTimer.addTimerEvent(countdownObserver);
			
		}

		@Override
		public Activity getActivity() {
			return this.activity;
		}
		
	}
}
